﻿<?xml version="1.0" encoding="utf-8"?>

<!-- *******************************************************************************************
     Copyright 2008 ZHENG Zhong <heavyzheng nospam-at gmail D0T com>
     - http://heavyz.blogspot.com/
     - http://buggarden.blogspot.com/
     
     $Id: siml-xhtml-plain.xsl 139 2008-07-23 22:38:15Z heavyzheng $
     ******************************************************************************************* -->

<xsl:stylesheet xmlns="http://www.w3.org/1999/xhtml" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

  <xsl:output method="html"/>
  
  <!-- top-level template ********************************************************************** -->
  
  <xsl:template match="siml">
    <html>
      <head>
        
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <xsl:apply-templates select="info" mode="xhtml-head"/>
        <link href="http://buggarden.googlecode.com/svn/siml/siml-xhtml-plain.css" type="text/css" rel="stylesheet"/>
        <!--link href="http://www.google.com/favicon.ico" type="image/x-icon" rel="shortcut icon"/-->
        
        <!-- include syntax highlighter 1.5.14 -->
        <link rel="stylesheet" type="text/css" href="http://buggarden.googlecode.com/svn/libs/SyntaxHighlighter-1.5.1/Styles/SyntaxHighlighter.css"/>
        <script language="javascript" src="http://buggarden.googlecode.com/svn/libs/SyntaxHighlighter-1.5.1/Scripts/shCore.js"></script>
        <script language="javascript" src="http://buggarden.googlecode.com/svn/libs/SyntaxHighlighter-1.5.1/Scripts/shBrushCpp.js"></script>
        <script language="javascript" src="http://buggarden.googlecode.com/svn/libs/SyntaxHighlighter-1.5.1/Scripts/shBrushJava.js"></script>
        <script language="javascript" src="http://buggarden.googlecode.com/svn/libs/SyntaxHighlighter-1.5.1/Scripts/shBrushPython.js"></script>
        <script language="javascript" src="http://buggarden.googlecode.com/svn/libs/SyntaxHighlighter-1.5.1/Scripts/shBrushCSharp.js"></script>
        <script language="javascript" src="http://buggarden.googlecode.com/svn/libs/SyntaxHighlighter-1.5.1/Scripts/shBrushVb.js"></script>
        <script language="javascript" src="http://buggarden.googlecode.com/svn/libs/SyntaxHighlighter-1.5.1/Scripts/shBrushJScript.js"></script>
        <script language="javascript" src="http://buggarden.googlecode.com/svn/libs/SyntaxHighlighter-1.5.1/Scripts/shBrushSql.js"></script>
        <script language="javascript" src="http://buggarden.googlecode.com/svn/libs/SyntaxHighlighter-1.5.1/Scripts/shBrushCss.js"></script>
        <script language="javascript" src="http://buggarden.googlecode.com/svn/libs/SyntaxHighlighter-1.5.1/Scripts/shBrushXml.js"></script>
        
        <!-- include jQuery 1.2.6 -->
        <script language="javascript" src="http://buggarden.googlecode.com/svn/libs/jquery-1.2.6.min.js"></script>
       
      </head>
      
      <body>
        <div id="siml">
          <xsl:apply-templates select="info|content"/>
        </div>
        <script language="javascript">
          /**
           Render source code fragments using SyntaxHighlighter.
           Source code fragments should be placed in the following HTML tag:
           &lt;pre name='code' class='${lang}'&gt;...&lt;/pre&gt;
           For more information about SyntaxHighlighter, check:
             http://syntaxhighlighter.googlecode.com/
           */
          window.onload = function() {
            dp.SyntaxHighlighter.ClipboardSwf = 'http://buggarden.googlecode.com/svn/libs/SyntaxHighlighter-1.5.1/Scripts/shClipboard.swf';
            dp.SyntaxHighlighter.BloggerMode();
            dp.SyntaxHighlighter.HighlightAll('code');
          }
        </script>
      </body>
    </html>
  </xsl:template>
  
  <xsl:template match="info" mode="xhtml-head">
    <title><xsl:value-of select="title"/></title>
  </xsl:template>
  
  <xsl:template match="info">
    <div id="siml-info">
      <h1 class="siml-title"><xsl:value-of select="title"/></h1>
      <h1 class="siml-subtitle"><xsl:value-of select="subtitle"/></h1>
      <div class="siml-authors">
        <xsl:for-each select="authors/author">
          <xsl:value-of select="name"/>
        </xsl:for-each>
      </div>
    </div>
  </xsl:template>

  <xsl:template match="content">
    <div id="siml-content">
      <xsl:apply-templates/>
    </div>
  </xsl:template>
  
  <xsl:template match="section|sec">
    <div class="siml-section">
      <xsl:call-template name="create-id"/>
      <xsl:apply-templates/>
    </div>
  </xsl:template>
  
  <xsl:template match="subsection|sec2">
    <div class="siml-subsection">
      <xsl:call-template name="create-id"/>
      <xsl:apply-templates/>
    </div>
  </xsl:template>
  
  <xsl:template match="subsubsection|sec3">
    <div class="siml-subsubsection">
      <xsl:call-template name="create-id"/>
      <xsl:apply-templates/>
    </div>
  </xsl:template>
  
  <xsl:template match="appendix">
    <div class="siml-appendix">
      <xsl:call-template name="create-id"/>
      <xsl:apply-templates/>
    </div>
  </xsl:template>
  
  <xsl:template match="title">
    <h2 class="siml-title"><xsl:value-of select="."/></h2>
  </xsl:template>
  
  <xsl:template match="subtitle">
    <h2 class="siml-subtitle"><xsl:value-of select="."/></h2>
  </xsl:template>

  <xsl:template match="para">
    <div class="siml-para">
      <xsl:call-template name="create-id"/>
      <p><xsl:apply-templates/></p>
    </div>
  </xsl:template>

  <xsl:template match="link">
    <a>
      <xsl:attribute name="href"><xsl:value-of select="@ref"/></xsl:attribute>
      <xsl:apply-templates/>
    </a>
  </xsl:template>
  
  <xsl:template match="ordered-list|ol">
    <xsl:call-template name="create-id"/>
    <ol><xsl:apply-templates select="list-item"/></ol>
  </xsl:template>
  
  <xsl:template match="unordered-list|ul">
    <xsl:call-template name="create-id"/>
    <ul><xsl:apply-templates select="list-item"/></ul>
  </xsl:template>
  
  <xsl:template match="list-item|li">
    <li><xsl:apply-templates/></li>
  </xsl:template>
  
  <xsl:template match="code">
    <code><xsl:apply-templates/></code>
  </xsl:template>
  
  <xsl:template match="em">
    <xsl:choose>
      <xsl:when test="@style = 'strong'">
        <b><xsl:apply-templates/></b>
      </xsl:when>
      <xsl:when test="@style = 'underlined'">
        <u><xsl:apply-templates/></u>
      </xsl:when>
      <xsl:otherwise>
        <i><xsl:apply-templates/></i>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  
  <xsl:template match="tm">
    <span><xsl:value-of select="."/></span>
    <xsl:choose>
      <xsl:when test="@class = 'copyright'">&#169;</xsl:when>
      <xsl:when test="@class = 'registered'">&#174;</xsl:when>
      <xsl:when test="@class = 'service'"><sup>SM</sup></xsl:when>
      <xsl:otherwise><sup>TM</sup></xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  
  <xsl:template match="subscript|sub">
    <sub><xsl:apply-templates/></sub>
  </xsl:template>
  
  <xsl:template match="superscript|sup">
    <sup><xsl:apply-templates/></sup>
  </xsl:template>
  
  <xsl:template match="br">
    <br/>
  </xsl:template>

  <xsl:template match="source">
    <xsl:variable name="lang" select="@lang"/>
    <div class="siml-source">
      <xsl:call-template name="create-id"/>
      <xsl:call-template name="create-title"/>
      <xsl:for-each select="content">
        <pre name="code">
          <xsl:attribute name="class"><xsl:value-of select="$lang"/></xsl:attribute>
          <xsl:apply-templates/>
        </pre>
      </xsl:for-each>
    </div>
  </xsl:template>
  
  <xsl:template match="image">
    <div class="siml-image">
      <xsl:call-template name="create-id"/>
      <xsl:call-template name="create-title"/>
      <xsl:for-each select="link">
        <img>
          <xsl:attribute name="src"><xsl:value-of select="@ref"/></xsl:attribute>
          <xsl:attribute name="alt"><xsl:value-of select="@alt"/></xsl:attribute>
        </img>
      </xsl:for-each>
    </div>
  </xsl:template>
  
  <xsl:template match="quote">
    <span class="siml-quote"><xsl:apply-templates/></span>
  </xsl:template>
  
  <xsl:template match="block-quote">
    <div class="siml-block-quote">
      <xsl:call-template name="create-id"/>
      <xsl:call-template name="create-title"/>
    </div>
  </xsl:template>
  
  <xsl:template match="table">
    <div class="siml-table">
      <xsl:call-template name="create-id"/>
      <xsl:call-template name="create-title"/>
      <table>
        <xsl:choose>
          <xsl:when test="count(@width) = 1">
            <xsl:attribute name="width"><xsl:value-of select="@width"/></xsl:attribute>
          </xsl:when>
          <xsl:otherwise>
            <xsl:attribute name="width">100%</xsl:attribute>
          </xsl:otherwise>
        </xsl:choose>
        <xsl:apply-templates select="row|tr"/>
      </table>
    </div>
  </xsl:template>
  
  <xsl:template match="row|tr">
    <tr>
      <xsl:apply-templates select="cell|header-cell|td|th"/>
    </tr>
  </xsl:template>
  
  <xsl:template match="header-cell|th">
    <th>
      <xsl:for-each select="@width">
        <xsl:attribute name="width"><xsl:value-of select="."/></xsl:attribute>
      </xsl:for-each>
      <xsl:apply-templates/>
    </th>
  </xsl:template>
  
  <xsl:template match="cell|td">
    <td>
      <xsl:for-each select="@width">
        <xsl:attribute name="width"><xsl:value-of select="."/></xsl:attribute>
      </xsl:for-each>
      <xsl:apply-templates/>
    </td>
  </xsl:template>
  
  <xsl:template match="verbatim">
    <div class="siml-verbatim">
      <xsl:call-template name="create-id"/>
      <xsl:variable name="text">
        <xsl:apply-templates/>
      </xsl:variable>
      <xsl:call-template name="make-verbatim">
        <xsl:with-param name="text" select="$text"/>
      </xsl:call-template>
    </div>
  </xsl:template>
  
  <xsl:template name="make-verbatim">
    <xsl:param name="text"/>
    <xsl:call-template name="make-verbatim-recursive">
      <!-- &#160; is non-breaking space -->
      <xsl:with-param name="text" select="translate($text, ' ', '&#160;')"/>
    </xsl:call-template>
  </xsl:template>
  
  <xsl:template name="make-verbatim-recursive">
    <xsl:param name="text" select="''"/>
    <xsl:choose>
      <!-- &#10; is line break -->
      <xsl:when test="not(contains($text, '&#10;'))">
        <xsl:value-of select="$text"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:variable name="len" select="string-length($text)"/>
        <xsl:choose>
          <xsl:when test="$len = 1">
            <br/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:variable name="half" select="$len div 2"/>
            <xsl:call-template name="make-verbatim-recursive">
              <xsl:with-param name="text" select="substring($text, 1, $half)"/>
            </xsl:call-template>
            <xsl:call-template name="make-verbatim-recursive">
              <xsl:with-param name="text" select="substring($text, ($half + 1), $len)"/>
            </xsl:call-template>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  
  <!-- named templates ************************************************************************* -->
  
  <xsl:template name="create-id">
    <xsl:for-each select="@id">
      <xsl:attribute name="id"><xsl:value-of select="."/></xsl:attribute>
    </xsl:for-each>
  </xsl:template>
  
  <xsl:template name="create-title">
    <xsl:for-each select="title">
      <h3 class="siml-block-title"><xsl:apply-templates/></h3>
    </xsl:for-each>
  </xsl:template>

</xsl:stylesheet>


